<template>
	<view class="container h-auto">
		<desc-wrapper :title="title" :desc="desc" style="padding-top: 70rpx;">
			<template #desc>
				<view class="tabs">
					<view v-for="(item,idx) in navList" :key="idx"
						:class="`tabs_item ${idx==curTab?'tabs_item-active':''}`" @tap="tabChange(idx,$event)">
						<text>{{item.name}}</text>
					</view>
					<view class="overlay" :style="`transform: translateX(${curTab*100}%)`"></view>
				</view>
			</template>
		</desc-wrapper>
		<view class="content-wrap" style="">
			<scroll-view scroll-y class="content-box" @scrolltolower="scrollBottom">
				<view v-for="(item,idx) in goodList" class="goods-item" :key="idx" @tap="goDetail(item.id)">
					<view class="w-full" :style="{background:`url(${item.mainimg}) no-repeat center / cover`}"></view>
					<view class="excharge">
						<view class="enhance-text el1">{{ item.name }}</view>
						<text class="score-text">￥{{ item.price }}</text>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {
		shopCate,
		goodList
	} from "@/api/api.js"
	export default {
		data() {
			return {
				title: '商城',
				desc: '',
				curTab: 0,
				navList: [],
				page: 1,
				goodList: []
			}
		},
		onShow: async function() {
			await this.getNav()
			this.goodList = []
			await this.getGoodsList()
		},
		methods: {
			async getNav() {
				await shopCate({
					first_id: 0
				}).then(res => {
					this.navList = res.data.data
				})
			},
			tabChange(index) {
				this.curTab = index
				this.goodList = []
				this.page = 1
				this.getGoodsList()
			},
			goDetail(id) {
				uni.navigateTo({
					url: `/pages/shop/goodDetail/goodDetail?id=${id}`
				})
			},
			// 获取goodslist
			getGoodsList() {
				goodList({
					first_id: this.navList[this.curTab].id,
					second_id: '',
					cate_id: '',
					page: this.page,
					keywords: '',
					listorder: 'default'
				}).then(res => {
					if (res.data.data.data.length > 0) {
						this.goodList = this.goodList.concat(res.data.data.data);
					} else {
						this.page--;
						/* uni.showToast({
							title: this.$t('general.There'),
							icon: "none"
						}) */
					}
				})
			},
			scrollBottom() {
				console.log("滚动到底部了")
				this.page++
				this.getGoodsList()
			}
		}
	}
</script>
<style lang="scss" scoped>
	.container {
		position: relative;
		min-height: auto;
		height: 100vh;
		background-image: url('../../static/image/bg.png');
		background-size: 100% 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.content-wrap {
		flex: 1;
		margin-top: -70rpx;
		width: 100%;
		border-top-left-radius: 30px;
		border-top-right-radius: 30px;
		background-color: #fff;
		background: url('@/static/image/bg.png') #fff no-repeat 100% 100%;
		background-position: 0%;
		background-origin: padding-box;
		overflow: auto;
	}

	/deep/.uni-scroll-view-content {
		display: flex;
		flex-wrap: wrap;
		height: auto;
	}

	.content-box {
		box-sizing: border-box;
		padding: 20rpx 20rpx;
		// 上面往上挪了120所以要补120的距离
		display: flex;
		flex-wrap: wrap;
		height: 100%;
		width: 100%;
		overflow: auto;

		.goods-item {
			font-size: 18px;
			overflow: hidden;
			width: 50%;
			padding: 0 12rpx 40rpx;

			.w-full {
				width: 330rpx;
				height: 190rpx;
				border-radius: 30rpx;
				margin-bottom: 24rpx;
			}
		}
	}

	.tabs {
		position: relative;
		display: flex;
		margin: 0 32rpx;
		border-radius: 16px;
		background-color: rgba(255, 255, 255, 0.28);
		overflow: hidden;

		.tabs_item {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 14px;
			color: #333;

			text {
				position: relative;
				z-index: 999;
				color: #fff;
				display: inline-block;
				padding: 12rpx 0;
			}
		}

		.tabs_item-active {
			text {
				color: #333;
			}
		}

		.overlay:last-child {
			content: '';
			position: absolute;
			z-index: 888;
			left: 0;
			top: 0;
			right: 50%;
			bottom: 0;
			background-color: #fff;
			border-radius: 16px;
			transition: .3s;
		}

	}
</style>